<?php 
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use DB;
use Blade;
use Input;
use Redirect;

class IndexController extends Controller {

    
    /**
     * @author:Where
     * @email:liuzhaowei55@gmail.com
     * @dateTime:  2015-08-01-21:10:51
     * @description:获取账户
     *   
     */
    public function index(){

        $where = array();
        $where['display'] = 1;
        $accounts = DB::table('account')->where($where)->get();
        foreach ($accounts as $key => $value) {
            $accounts[$key]->time =  date('Y-m-d H:i:s',$value->time); 
        }
        return view('index')->with('accounts', $accounts);
    }

    /**
     * @author:Where
     * @email:liuzhaowei55@gmail.com
     * @dateTime:  2015-08-08 10-52:26
     * @description:获取流水详情
     *   
     */
    public function getDetails($id){
        //定义返回参数
        $returnData = array();
        //获取账户名称
        $where = array();
        $where['id'] = $id;
        $account = DB::table('account')->where($where)->first();
        //查询
        $where = array();
        $where['account'] = $id;
        $where['display'] = 1;
        $field = array('cat','amount','time','remark');
        $details = DB::table('detail')->where($where)->get($field);
        foreach ($details as $key => $value) {
            //时间格式化
            $details[$key]->time = date('Y-m-d H:i:s',$value->time);
            //获取流水名称
            $where = array();
            $where['id'] = $value->cat;
            $cat = DB::table('cat')->where($where)->first();
            $details[$key]->cat = $cat->title;
            $details[$key]->account = $account->title;
        }
        return view('detail')->with('details',$details)->with('account',$account);
    }

    /**
     * @author:Where
     * @email:liuzhaowei55@gmail.com
     * @dateTime:  2015-08-08 10:52:56
     * @description:添加流水详情
     *   
     */
    public function addDetail($account){
        $returnData = array();
        //获取账户名称
        $where = array();
        $where['id'] = $account;
        $account = DB::table('account')->where($where)->first();
        //获取流水分类 
        $cats = DB::table('cat')->get();

        $returnData['account'] = $account;
        $returnData['cats'] = $cats;

        return view('addDetail')->with('returnData',$returnData);
    }

    /**
     * @author:Where
     * @email:liuzhaowei55@gmail.com
     * @dateTime:  2015-08-08 12:07:10
     * @description:保存流水详情
     *   
     */
     
    public function saveDetail(){
        //前台数据
        $detail = array();
        $detail['account'] = Input::get('account');
        $detail['cat'] = Input::get('cat');
        $detail['amount'] = Input::get('amount');
        $detail['time'] = time();

        //更新账户数据
        $where = array();
        $where['id'] = $detail['account'];
        $account = DB::table('account')->where($where)->first();
        $data = array();
        $data['amount'] = $account->amount + $detail['amount'];

        //保存
        DB::table('account')->where($where)->update($data);
        DB::table('detail')->insert($detail);
        return Redirect::to('/');
    }

}